﻿//<TunynetCopyright>
//--------------------------------------------------------------
//<copyright>青岛拓宇网络科技有限公司 2005-2008</copyright>
//<version>V0.5</verion>
//<createdate>2008-2-22</createdate>
//<author>zhengw</author>
//<email>zhengw@tunynet.com</email>
//<log date="2008-2-22">创建</log>
//--------------------------------------------------------------
//</TunynetCopyright>

using System;
using System.Collections.Generic;
using System.Text;
using Tunynet.QingdaoCustoms.Serialization;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;

namespace ChinaCustoms.Qingdao.Workflow.Supports
{
    /// <summary>
    /// 活动元数据实体类
    /// </summary>
    [Serializable]
    public class ActivityPrototypeInfo : ExtendedAttributes
    {
        private Guid prototypeGuid;
        /// <summary>
        /// 活动原型GUID
        /// </summary>
        public Guid PrototypeGuid
        {
            get { return this.prototypeGuid; }
            set { this.prototypeGuid = value; }
        }

        private Guid workflowGuid;
        /// <summary>
        /// 工作流GUID
        /// </summary>
        public Guid WorkflowGuid
        {
            get { return this.workflowGuid; }
            set { this.workflowGuid = value; }
        }

        private Guid subFlowGuid = Guid.Empty;
        /// <summary>
        /// 子流程GUID
        /// </summary>
        public Guid SubFlowGuid
        {
            get { return subFlowGuid; }
            set { subFlowGuid = value; }
        }

        private string activityName = string.Empty;
        /// <summary>
        /// 活动名称
        /// </summary>
        public string ActivityName
        {
            get { return this.activityName; }
            set { this.activityName = value; }
        }

        private string codeName = string.Empty;
        /// <summary>
        /// 活动编码
        /// </summary>
        public string CodeName
        {
            get { return this.codeName; }
            set { this.codeName = value; }
        }

        private int activityLevel;
        /// <summary>
        /// 活动级别
        /// </summary>
        public int ActivityLevel
        {
            get { return activityLevel; }
            set { activityLevel = value; }
        }

        private string description = string.Empty;
        /// <summary>
        /// 活动描述
        /// </summary>
        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }

        private string urlOfWorkItem = string.Empty;
        /// <summary>
        /// 处理或浏览该活动的工作项时调用的URL
        /// </summary>
        public string UrlOfWorkItem
        {
            get { return urlOfWorkItem; }
            set { urlOfWorkItem = value; }
        }

        private string activityCodeNamesForJump = string.Empty;
        /// <summary>
        /// 可跳转到的活动编码
        /// </summary>
        public string ActivityCodeNamesForJump
        {
            get { return this.activityCodeNamesForJump; }
            set { this.activityCodeNamesForJump = value; }
        }

        private bool allowBack = true;
        /// <summary>
        /// 是否允许回退
        /// </summary>
        public bool AllowBack
        {
            get { return allowBack; }
            set { allowBack = value; }
        }


        private bool allowFinalJudgment = false;
        /// <summary>
        /// 是否允许终审
        /// </summary>
        public bool AllowFinalJudgment
        {
            get { return this.allowFinalJudgment; }
            set { this.allowFinalJudgment = value; }
        }

        private bool isSubFlow = false;
        /// <summary>
        /// 是否子流程
        /// </summary>
        public bool IsSubFlow
        {
            get { return this.isSubFlow; }
            set { this.isSubFlow = value; }
        }

        private string requiredDepartmentIDs = string.Empty;
        /// <summary>
        /// 活动对应的直接部门
        /// </summary>
        public string RequiredDepartmentIDs
        {
            get { return this.requiredDepartmentIDs; }
            set { this.requiredDepartmentIDs = value; }
        }

        private RelativeDepartmentLevel requiredRelativeDepartment = 0;
        /// <summary>
        /// 活动对应的相对部门
        /// </summary>
        public RelativeDepartmentLevel RequiredRelativeDepartment
        {
            get { return this.requiredRelativeDepartment; }
            set { this.requiredRelativeDepartment = value; }
        }

        private string requiredRoles = string.Empty;
        /// <summary>
        /// 活动对应的角色
        /// </summary>
        public string RequiredRoles
        {
            get { return this.requiredRoles; }
            set { this.requiredRoles = value; }
        }

        private string requiredUserGroups = string.Empty;
        /// <summary>
        /// 活动对应的用户组
        /// </summary>
        public string RequiredUserGroups
        {
            get { return this.requiredUserGroups; }
            set { this.requiredUserGroups = value; }
        }

        private string requiredUserNames = string.Empty;
        /// <summary>
        /// 活动对应的用户名
        /// </summary>
        public string RequiredUserNames
        {
            get { return requiredUserNames; }
            set { this.requiredUserNames = value; }
        }

        private bool allowRedesignateExecutor = true;
        /// <summary>
        /// 是否允许在运行时指定执行人
        /// </summary>
        public bool AllowRedesignateExecutor
        {
            get { return this.allowRedesignateExecutor; }
            set { this.allowRedesignateExecutor = value; }
        }

        private bool allowPostil = false;
        /// <summary>
        /// 是否允许审批意见
        /// </summary>
        public bool AllowPostil
        {
            get
            {
                if (IsSubFlow)
                    return true;
                else
                    return this.allowPostil;
            }
            set { this.allowPostil = value; }
        }

        //private bool allowAttachment = false;
        ///// <summary>
        ///// 是否允许上传附件
        ///// </summary>
        //public bool AllowAttachment
        //{
        //    get { return this.allowAttachment; }
        //    set { this.allowAttachment = value; }
        //}


        #region 序列化属性

        /// <summary>
        /// 是否需要子流程反馈（仅适用于子流程活动）
        /// </summary>
        public bool RequireFeedbackInSubFlow
        {
            get { return GetBool("RequireFeedbackInSubFlow", true); }
            set { SetExtendedAttribute("RequireFeedbackInSubFlow", value.ToString()); }
        }

        /// <summary>
        /// 分发子流程并办结（仅适用于子流程活动）
        /// </summary>
        /// <remarks>
        /// 子流程分发后主流程停止等待所有子流程完成后办结整个工作流实例
        /// </remarks>
        public bool StartSubFlowAndStopThisFlow
        {
            get { return GetBool("StartSubFlowAndStopThisFlow", true); }
            set { SetExtendedAttribute("StartSubFlowAndStopThisFlow", value.ToString()); }
        }

        /// <summary>
        /// 发起子流程打开部门选择器时是否只显示相对顶级部门（仅适用于子流程活动）
        /// </summary>
        public bool OnlyShowRelativeTopLevelInOrganizationSelectorOfSubFlow
        {
            get { return GetBool("OnlyShowRelativeTopLevelInOrganizationSelectorOfSubFlow", false); }
            set { SetExtendedAttribute("OnlyShowRelativeTopLevelInOrganizationSelectorOfSubFlow", value.ToString()); }
        }

        /// <summary>
        /// 启动该子流程活动的活动Guid
        /// </summary>
        public Guid ActivityGuidOfStartThisSubFlowActivity
        {
            get { return GetGuid("ActivityGuidOfStartThisSubFlowActivity", Guid.Empty); }
            set { SetExtendedAttribute("ActivityGuidOfStartThisSubFlowActivity", value.ToString()); }
        }

        /// <summary>
        /// 子流程活动创建的子流程SubFlowGuid
        /// </summary>
        public Guid CorrelativeSubFlowGuid
        {
            get { return GetGuid("CorrelativeSubFlowGuid", Guid.Empty); }
            set { SetExtendedAttribute("CorrelativeSubFlowGuid", value.ToString()); }
        }

        #endregion


        #region 扩展属性

        /// <summary>
        /// 是否有足够的依据分配给执行者
        /// </summary>
        public bool CanAssignToExecutor(IUser currentUser,string applicationCodeName)
        {
            if (string.IsNullOrEmpty(RequiredUserNames) == false)
            {
                //TODO:最后还应再去判断一下这些用户是否真实存在
                return true;
            }

            if (string.IsNullOrEmpty(RequiredRoles) == false)
            {
                string organizationIDs = null;
                if (this.RequiredRelativeDepartment != RelativeDepartmentLevel.NotSet)
                {
                    if (currentUser != null)
                    {
                        if (this.RequiredRelativeDepartment == RelativeDepartmentLevel.RelativeOneLevel)
                        {
                            //organizationIDs = new string[1];
                            if (currentUser.Parent != null)
                            {
                                organizationIDs = currentUser.Parent.ID;
                            }
                            else
                            {
                                organizationIDs = string.Empty;
                            }
                        }
                        else if (this.RequiredRelativeDepartment == RelativeDepartmentLevel.RelativeTopLevel)
                        {
                            //organizationIDs = new string[1];
                            if (currentUser.TopOU != null)
                            {
                                organizationIDs = currentUser.TopOU.ID;
                            }
                            else
                            {
                                organizationIDs = string.Empty;
                            }
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(this.RequiredDepartmentIDs.Trim()))
                {
                    organizationIDs = this.RequiredDepartmentIDs;
                }

                return ActivityPrototypes.HasUsersInRoles(RequiredRoles,organizationIDs,applicationCodeName);
            }
            else
            {
                return false;
            }
        }




        /// <summary>
        /// 是不是工作流模板中的第一个活动
        /// </summary>
        public bool IsFirstActivityPrototype
        {
            get { return this.ActivityLevel == 0; }
        }

        #endregion


    }
}
